<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionBack\Controller;

use AuctionCore\Common\AjaxPage;
use AuctionCore\System\SystemConf;

/**
 * 红包管理
 *
 * @author Xmh
 */
class RewardCashLogController extends BaseController
{

    protected $rewardLogLogic;

    private $empId;

    public function __construct()
    {
        parent::__construct();
        $this->rewardLogLogic = D('RewardLog', 'Logic');

        $this->empId = $this->user_info['emp_id'];
        //模板赋值
        $this->assign("emp_id", $this->empId);
    }

    public function index()
    {
        $this->display();
    }

    /**
     * 红包列表
     */
    public function rewardCashLogList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['c_id'] = I('get.c_id/d');
        $params['c_type'] = I('get.c_type/d');
        $params['agent_uid'] = I('get.agent_uid/d');
        $params['agent_phone'] = I('get.agent_phone');
        $params['car_no'] = I('get.car_no');
        $params['start_time'] = I('get.start_time');
        $params['end_time'] = I('get.end_time');
        $result = $this->rewardLogLogic->getRewardCashLogList($page, $size, $params);
//        dump($result);exit;
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->assign('total', $result['total']);
        $this->assign('reward_total', $result['reward_total']);
        $this->display();
    }

    /**
     * 红包规则页
     */
    public function rewardRuleConfig()
    {
        $saleCarAward = SystemConf::getConfByName("SALE_CAR_AWARD");
        $saleCarAwardConf = json_decode($saleCarAward['value'], true);
        $data['cash'] = $saleCarAwardConf['cash']['amount'];
        $data['agent_cash_per'] = $saleCarAwardConf['cash']['rate'] * 100;
        $data['superior_cash_per'] = 100 - $saleCarAwardConf['cash']['rate'] * 100;
        $data['cash_isAward'] = $saleCarAwardConf['cash']['isAward'];
        $data['integral'] = $saleCarAwardConf['integral']['amount'];
        $data['agent_integral_per'] = $saleCarAwardConf['integral']['rate'] * 100;
        $data['superior_integral_per'] = 100 - $saleCarAwardConf['integral']['rate'] * 100;
        $data['integral_isAward'] = $saleCarAwardConf['integral']['isAward'];
        $data['growth'] = $saleCarAwardConf['growth']['amount'];
        $data['agent_growth_per'] = $saleCarAwardConf['growth']['rate'] * 100;
        $data['superior_growth_per'] = 100 - $saleCarAwardConf['growth']['rate'] * 100;
        $data['growth_isAward'] = $saleCarAwardConf['growth']['isAward'];
        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 更新红包分成比例规则
     */
    public function updateRewardRuleConfig()
    {
        $rewardCash = I('post.reward_cash/d', 0);
        $agentCashPer = I('post.agent_cash_per');
        $superiorCashPer = I('post.superior_cash_per');
        $cashIsAward = I('post.cash_isAward/d', 0);
        $rewardIntegral = I('post.reward_integral/d');
        $agentIntegralPer = I('post.agent_integral_per');
        $superiorIntegralPer = I('post.superior_integral_per');
        $integralIsAward = I('post.integral_isAward/d', 0);
        $rewardGrowth = I('post.reward_growth/d', 0);
        $agentGrowthPer = I('post.agent_growth_per');
        $superiorGrowthPer = I('post.superior_growth_per');
        $growthIsAward = I('post.growth_isAward/d', 0);
        if (empty($rewardCash)) {
            $this->helpjsonresult("400", '红包金额不能为空');
        }
        $cash_match = "/(^[0-9]\d*$)/";
        if (!preg_match($cash_match, $rewardCash)) {
            $this->helpjsonresult("400", '红包金额格式不正确');
        }
        if (!preg_match($cash_match, $rewardIntegral)) {
            $this->helpjsonresult("400", '积分总额格式不正确');
        }
        if (!preg_match($cash_match, $rewardGrowth)) {
            $this->helpjsonresult("400", '成长值总额格式不正确');
        }
        if (empty($agentCashPer) || empty($agentIntegralPer) || empty($agentGrowthPer)) {
            $this->helpjsonresult("400", '经纪人占比不能为空');
        }
        $per_match = "/^(?:0|[1-9][0-9]?|100)$/";
        if (!preg_match($per_match, $agentCashPer) ||
            !preg_match($per_match, $agentIntegralPer) ||
            !preg_match($per_match, $agentGrowthPer)
        ) {
            $this->helpjsonresult("400", '经纪人占比不正确');
        }
        if (empty($superiorCashPer) || empty($superiorIntegralPer) || empty($superiorGrowthPer)) {
            $this->helpjsonresult("400", '上级占比不能为空');
        }
        if (!preg_match($per_match, $superiorCashPer) ||
            !preg_match($per_match, $superiorIntegralPer) ||
            !preg_match($per_match, $superiorGrowthPer)
        ) {
            $this->helpjsonresult("400", '上级占比不正确');
        }
        $cash_per = $agentCashPer + $superiorCashPer;
        $integral_per = $agentIntegralPer + $superiorIntegralPer;
        $growth_per = $agentGrowthPer + $superiorGrowthPer;
        if ($cash_per != 100 || $integral_per != 100 || $growth_per != 100) {
            $this->helpjsonresult("400", '分成比例设置不正确');
        }
        $data['cash']['amount'] = $rewardCash;
        $data['cash']['rate'] = $agentCashPer / 100;
        $data['cash']['isAward'] = $cashIsAward;
        $data['integral']['amount'] = $rewardIntegral;
        $data['integral']['rate'] = $agentIntegralPer / 100;
        $data['integral']['isAward'] = $integralIsAward;
        $data['growth']['amount'] = $rewardGrowth;
        $data['growth']['rate'] = $agentGrowthPer / 100;
        $data['growth']['isAward'] = $growthIsAward;
        $value = json_encode($data);
        $res = SystemConf::updateConfByValue("SALE_CAR_AWARD", $value);
        if ($res === false) {
            $this->helpjsonresult('500', '更新失败');
        }
        $this->helpjsonresult('200', '更新成功');
    }
}
